package com.hui.controller;

import com.hui.config.Result;
import com.hui.entity.Book;
import com.hui.entity.News;
import com.hui.mapper.NewsMapper;
import com.hui.service.NewsService;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.logging.stdout.StdOutImpl;
import org.apache.tomcat.util.json.JSONParser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @BelongsProject: demo
 * @BelongsPackage: com.hui.controller
 * @Author: HUI
 * @CreateTime: 2021-12-08 11:40
 * @Description:
 */

@RestController
@RequestMapping("/news")
@Slf4j
public class NewsController {

    @Autowired
    NewsService newsService;

    //分页查询数据
    @GetMapping
    public Result<?> queryBook(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String searchStr
    ) {

        return Result.success(newsService.queryBook(pageNum, pageSize, searchStr));
    }


    //新增
    @PostMapping
    public Result<?> saveBook(@RequestBody News news) {
        return newsService.saveUser(news) > 0 ? Result.success(news.getTitle()) : Result.error("-1", "该文章已经存在");

    }


    //修改
    @PutMapping
    public Result<?> updateUser(@RequestBody News news) {
        int update = newsService.updateBook(news);
        return update > 0 ? Result.success(news.getTitle()) : Result.error("-1", news.getTitle() + "更新失败！");
    }


    //删除下架
    @DeleteMapping
    public Result<?> deleteBook(@RequestParam Integer id) {

        return newsService.deleteBook(id) > 0 ? Result.success() : Result.error("-1", "删除失败");

    }


    //查询自己发布的新闻
    @GetMapping("/queryNews")
    public Result<?> QueryPushNews(@RequestParam String author){
        List<News> news = newsService.QueryPushNews(author);
        return Result.success(news);
    }

}
